package com.wgchao.identityrightsmanagement.query.vo;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAgency;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAgencyRepository;
import com.wgchao.identityrightsmanagement.infrastructure.mapstruct.OfficeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: OfficeVoConvert
 * @author: wgchao
 * @createTime: 2020/6/16 4:42 PM
 * @Description: TODO
 */
@Component
public class OfficeVoConvert {
    @Autowired
    private OfficeMapper officeMapper;
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;
    @Autowired
    private SysBaseAgencyRepository sysBaseAgencyRepository;

    public OfficeVo convert(SysBaseOffice sysBaseOffice){
        OfficeVo officeVo = officeMapper.toT(sysBaseOffice);
        if(!StringUtils.isEmpty(sysBaseOffice.getCreator())){
            SysBaseAccount creator = sysBaseAccountRepository.findFirstByUsername(sysBaseOffice.getCreator());
            officeVo.setCreatorName(creator == null ? null : creator.getName());
        }
        if(!StringUtils.isEmpty(sysBaseOffice.getLastUpdator())){
            SysBaseAccount updator = sysBaseAccountRepository.findFirstByUsername(sysBaseOffice.getLastUpdator());
            officeVo.setLastUpdatorName(updator == null ? null : updator.getName());
        }
        SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCode(sysBaseOffice.getAgencyCode());
        officeVo.setAgencyName(sysBaseAgency == null ? null : sysBaseAgency.getAgencyName());
        return officeVo;
    }


}
